package m.toolkit.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import m.framework.controller.BaseController;
import m.framework.controller.ResponseView;
import m.framework.controller.Task;
import m.toolkit.file.model.FileInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

/**
 * @author ming
 * @date 2014年2月22日下午4:32:10
 */
@Controller
@Task("文件")
public class FileInfoController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(FileInfoController.class);

	
	/** 下载 */
	@Task("下载文件")
    public ResponseView download(FileInfo fileInfo) {
        FileInfo info = this.defultService().get(FileInfo.class, fileInfo.getFileInfoId());
        return new FileDownloadView(info);
    }
	
	
	/** 文件下载的视图 */
    public static class FileDownloadView implements ResponseView {
        private FileInfo fileInfo;

        public FileDownloadView(FileInfo fileInfo) {
            this.fileInfo = fileInfo;
        }

        public void parseResponse(HttpServletResponse response) {
            byte[] buffer = null;
            String filename = null;
            InputStream fis = null;
            try {
                filename = fileInfo.getFileName();
                // 以流的形式下载文件。
                fis = new BufferedInputStream(new FileInputStream(fileInfo.getDownLoadPath()));
                buffer = new byte[fis.available()];
                fis.read(buffer);
                // 清空response
                response.reset();
            } catch (Exception ex) {
            	log.error("向页面写文件出错:"+ex.getMessage(),ex);
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                    	log.error("向页面写文件出错:"+e.getMessage(),e);
                    }
                }
            }
            OutputStream toClient = null;
            try {
                if (buffer == null) {
                    return;
                }
                // 设置response的Header
                String newName = URLEncoder.encode(filename, "UTF-8");
                response.addHeader("Content-Disposition", "attachment;filename=" + newName);
                response.addHeader("Content-Length", "" + fileInfo.getSize());
                toClient = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");
                toClient.write(buffer);
                toClient.flush();
            } catch (IOException ex) {
            	log.error("向页面写文件出错:"+ex.getMessage(),ex);
            } finally {
                if (toClient != null) {
                    try {
                        toClient.close();
                    } catch (IOException e) {
                    	log.error("向页面写文件出错:"+e.getMessage(),e);
                    }
                }
            }
        }
    }
}
